package com.vdg.fps.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.vdg.R;
import com.vdg.fps.data.FaceBookAccount;
import com.vdg.fps.data.GroupsData;
import com.vdg.fps.ui.view.TextViewPlus;
import com.vdg.fps.ulti.Utilities;

public class ListLikedPagesAdapter extends ArrayAdapter<GroupsData> {
	private List<GroupsData> mListGroups;
	private ArrayList<GroupsData> mOrgListGroups;

	public ListLikedPagesAdapter(Context context, int textViewResourceId,
			List<GroupsData> objects) {
		super(context, textViewResourceId, objects);
		mOrgListGroups = new ArrayList<GroupsData>();
		mListGroups = objects;
		mOrgListGroups.addAll(mListGroups);
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		GroupsData group = mListGroups.get(position);
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(getContext()).inflate(
					R.layout.liked_row, parent, false);
			holder.mTxvName = (TextViewPlus) convertView
					.findViewById(R.id.txv_name);
			holder.mTxvID = (TextViewPlus) convertView
					.findViewById(R.id.txv_id);
			holder.mTxvCanPost = (TextViewPlus) convertView
					.findViewById(R.id.txv_can_post);
			holder.mTxvCategory = (TextViewPlus) convertView
					.findViewById(R.id.txv_category);
			holder.mTxvLikes = (TextViewPlus) convertView
					.findViewById(R.id.txv_likes);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (group != null) {
			String name = group.getName();
			String id = group.getID();
			boolean canpost = group.isCanPost();
			if (canpost) {
				holder.mTxvCanPost.setText("YES");
			} else {
				holder.mTxvCanPost.setText("NO");
			}
			holder.mTxvName.setText(name);
			holder.mTxvID.setText(id);
			holder.mTxvCategory.setText(group.getCategory());
			holder.mTxvLikes.setText(group.getLikes());
		}
		return convertView;
	}

	public static class ViewHolder {
		private TextViewPlus mTxvName;
		private TextViewPlus mTxvID;
		private TextViewPlus mTxvLikes;
		private TextViewPlus mTxvCategory;
		private TextViewPlus mTxvCanPost;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListGroups.size();
	}

	public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		mListGroups.clear();
		if (charText.length() == 0) {
			mListGroups.addAll(mOrgListGroups);
		} else {
			for (GroupsData wp : mOrgListGroups) {
				if (wp.getName().toLowerCase(Locale.getDefault())
						.contains(charText)) {
					mListGroups.add(wp);
				}
			}
		}
		notifyDataSetChanged();
	}

	public GroupsData getItem(int position) {
		if (mListGroups == null || mListGroups.size() == 0) {
			return null;
		}
		return mListGroups.get(position);
	}

	public List<GroupsData> getDisplayData() {
		return mListGroups;
	}
}
